package com.ruicar.afs.cloud.auth.feign;

import com.alibaba.fastjson.JSONObject;
import com.ruicar.afs.cloud.common.core.constant.CommonConstants;
import com.ruicar.afs.cloud.common.core.security.service.AfsSysAdminRootService;
import feign.RequestInterceptor;
import feign.RequestTemplate;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

@Component
@AllArgsConstructor
@Slf4j
public class AfsAuthRequestInterceptor implements RequestInterceptor {
    private AfsSysAdminRootService afsSysAdminRootService;
    @Override
    public void apply(RequestTemplate template) {
        try {
            JSONObject tokenInfo = afsSysAdminRootService.getCacheToken();
            if(tokenInfo.containsKey(CommonConstants.ACCESS_TOKEN)){
                template.header(CommonConstants.AUTHORIZATION, CommonConstants.BEARER + tokenInfo.getString(CommonConstants.ACCESS_TOKEN));
            }
        }catch (Exception e){
            log.error("",e);
        }
    }
}
